package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.YongHuJSDao;
import net.mysoft.budgetsoft.system.domain.TYongHuJS;
import net.mysoft.budgetsoft.system.pagemodel.JueSe;
import net.mysoft.budgetsoft.system.pagemodel.YongHuJS;
import net.mysoft.budgetsoft.system.service.YongHuJSService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 
 * @author dagongPGP v1.0
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a>
 * 
 * @version $Revision: 1.00 $Date: 2013-12-27 13:47:58
 */
@Service("yongHuJSService")
public class YongHuJSServiceImpl implements YongHuJSService {
	@Resource
	private YongHuJSDao yongHuJSDao;

	public YongHuJSDao getYongHuJSDao() {
		return this.yongHuJSDao;
	}

	public void setYongHuJSDao(YongHuJSDao yongHuJSDao) {
		this.yongHuJSDao = yongHuJSDao;
	}

	@Override
	public void add(YongHuJS yongHuJS) {
		TYongHuJS t = new TYongHuJS();
		BeanUtils.copyProperties(yongHuJS, t);
		t.setBianHao(UUID.randomUUID().toString());
		yongHuJSDao.save(t);
	}

	@Override
	public void edit(YongHuJS yongHuJS) {
		TYongHuJS t = new TYongHuJS();
		BeanUtils.copyProperties(yongHuJS, t);
		yongHuJSDao.update(t);
	}

	@Override
	public void delete(YongHuJS yongHuJS) {
		for (String id : yongHuJS.getIds().split(",")) {
			Object t = yongHuJSDao.get(TYongHuJS.class, id);
			if (t != null) {
				yongHuJSDao.delete(t);
			}
		}
	}

	@Override
	public DataGrid datagrid(YongHuJS yongHuJS) {
		DataGrid dg = new DataGrid();
		String hql = "from TYongHuJS t ";
		List params = new ArrayList();
		hql = addWhere(yongHuJS, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(yongHuJS, hql);
		List<TYongHuJS> toList = yongHuJSDao.find(hql, params, yongHuJS.getPage(), yongHuJS.getRows());
		List<YongHuJS> oList = new ArrayList<YongHuJS>();
		oList = changeModel(toList);
		dg.setTotal(yongHuJSDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}

	private String addWhere(YongHuJS yongHuJS, String hql, List<Object> params) {
		return hql;
	}

	private String addOrder(YongHuJS yongHuJS, String hql) {
		if (yongHuJS.getSort() != null) {
			hql += " order by " + yongHuJS.getSort() + " " + yongHuJS.getOrder();
		}
		return hql;
	}

	public List<YongHuJS> changeModel(List<TYongHuJS> tYongHuJS) {
		List<YongHuJS> list = new ArrayList<YongHuJS>();
		for (TYongHuJS to : tYongHuJS) {
			YongHuJS o = new YongHuJS();
			BeanUtils.copyProperties(to, o);
			list.add(o);
		}
		return list;
	}

	@Override
	public List<JueSe> findJueSeByYongHuBH(String YongHuBH) {
		List<TYongHuJS> toList = yongHuJSDao.find("from TYongHuJS t left join fetch t.tJueSe where t.tYongHu.bianHao=?", new Object[] { YongHuBH });
		List<JueSe> trList = new ArrayList<JueSe>();
		for (TYongHuJS ur : toList) {
			JueSe jueSe = new JueSe();
			BeanUtils.copyProperties(ur.gettJueSe(), jueSe);
			trList.add(jueSe);
		}
		return trList;
	}
}
